//
//  TJMainController.m
//  BaoDian
//
//  Created by mac on 2024/7/17.
//

#import "TJMainController.h"
#import "TJNavigationController.h"
#import "TJTemplateController.h"
#import "TJPersonalCenterControler.h"
#import "TJMerchantPersonalCenterController.h"

@interface TJMainController ()<UITabBarControllerDelegate>

@property (strong, nonatomic) NSMutableArray *tabItemConfigurations;
@property (nonatomic, strong) NSDate *lastDate;
@property (nonatomic, assign) NSInteger lastClickIndex;

@end

@implementation TJMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setingTabBar];
    [self addChildViewController];
}


- (void)setingTabBar {
    
    self.delegate = self;

    self.tabBar.barTintColor = UIColor.tj_backgroundColor;
    self.tabBar.backgroundColor = UIColor.tj_backgroundColor;
    self.tabBar.translucent = NO;
    
    self.tabBar.layer.shadowColor = [UIColor.blackColor colorWithAlphaComponent:0.3].CGColor;
    self.tabBar.layer.shadowOffset = CGSizeMake(0, 0);
    self.tabBar.layer.shadowRadius = 5;
    self.tabBar.layer.shadowOpacity = 0.3;
    
    if (@available(iOS 13.0, *)) {
        [[UITabBar appearance] setTintColor:UIColor.tj_tabbarSelectColor];
        [[UITabBar appearance] setUnselectedItemTintColor:UIColor.tj_tabbarUnSelectColor];
        [UITabBar appearance].shadowImage = [UIImage new];
        [UITabBar appearance].backgroundImage = [UIImage imageWithColor:UIColor.clearColor];
        
    } else {
        self.tabBar.shadowImage = [UIImage new];
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColor.tj_tabbarUnSelectColor} forState:UIControlStateNormal];
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColor.tj_tabbarSelectColor} forState:UIControlStateSelected];
    }
    
    self.view.backgroundColor = UIColor.tj_backgroundColor;
}


- (void)addChildViewController {
    [self.tabItemConfigurations enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self addChildViewController:obj[@"controller"] image:obj[@"iconName"]  selectImage:obj[@"selectIconName"] title:obj[@"title"]];
    }];
}


- (void)addChildViewController:(UIViewController *)childController image:(NSString *)image  selectImage:(NSString *)selectImage title:(NSString *)title {
    
    TJNavigationController *navigationController = [[TJNavigationController alloc] initWithRootViewController: childController];
    navigationController.view.backgroundColor = [UIColor whiteColor];
    [self addChildViewController: navigationController];
    childController.title = title;
    childController.navigationItem.title = title;
    childController.tabBarItem.image = [UIImage imageNamed:image];
    childController.tabBarItem.selectedImage = [UIImage imageNamed:selectImage];
}


- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [UIView setAnimationsEnabled:NO];
    NSLog(@"setAnimationsEnabled-false");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(200 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
        [UIView setAnimationsEnabled:YES];
        NSLog(@"setAnimationsEnabled-YES");
    });

}


- (NSMutableArray *)tabItemConfigurations {
    if (!_tabItemConfigurations) {
        _tabItemConfigurations = @[
            @{
                @"controller": [[TJTemplateController alloc] init],
                @"iconName": @"plaza",
                @"selectIconName": @"plaza_select",
                @"title": @"模版广场",
            }
        ].mutableCopy;
        
        if (TJUserInfo.isMerchant) {
            [_tabItemConfigurations addObject:@{
                @"controller": [[TJMerchantPersonalCenterController alloc] init],
                @"iconName": @"mine",
                @"selectIconName": @"mine_select",
                @"title": @"商家中心",
            }];
        } else {
            [_tabItemConfigurations addObject:@{
                @"controller": [[TJPersonalCenterControler alloc] init],
                @"iconName": @"mine",
                @"selectIconName": @"mine_select",
                @"title": @"个人中心",
            }];
        }
    }
    return _tabItemConfigurations;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
